<?xml version="1.0" encoding="UTF-8" ?>

<!-- 
One XLST that use HTML + CSS to display a table with all the stadiums showing 
for each stadium the number of games which took place there with the total 
number of spectators.
-->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="xml" indent="yes"/>
	
	<xsl:template match="/">
	<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="xslt/stadiums_page.xslt"</xsl:processing-instruction>
	
	<stadiums>

      <xsl:for-each select="/league/stadiums/stadium">
      <xsl:variable name="currentStadium" select="@id"></xsl:variable>
      <xsl:variable name="gamesHeld" select="count(/league/fixtures/game/game_stadium[@stadiumId= $currentStadium])" />
      <xsl:variable name="totalSpectators" select="sum(/league/fixtures/game[./game_stadium[@stadiumId=$currentStadium]]/attendance)" />
        <stadium>
        	<xsl:attribute name="id" select="$currentStadium" />
          <name><xsl:value-of select="stadiumName"/></name>
          <gamesHeld><xsl:value-of select="$gamesHeld"/></gamesHeld>          
          <totalSpectators><xsl:value-of select="$totalSpectators" /></totalSpectators>
          <averageSpectators><xsl:value-of select="($totalSpectators) div $gamesHeld" /></averageSpectators>
        </stadium>
      </xsl:for-each>
	</stadiums>

	</xsl:template>

</xsl:stylesheet>
